package westh.ilib.struts.action.manage.question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.ilib.bean.manage.QuestionBean;
import westh.ilib.bean.manage.ResourceBean;
import westh.util.YyAction;

public class QuestionInfoAction extends YyAction {
	//define commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(QuestionInfoAction.class);

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// check log and authority,don`s delete
		westh.log.YyLog.addFootprint(request, "动作-显示问题详细信息");
		String resourceId = request.getParameter("Id");
		// dbxml name
		String dbName = "question.dbxml";
		//put in data into json string
		JSONObject inJson = new JSONObject();
		inJson.put("resourceId", resourceId);
		inJson.put("dbName", dbName);
		String inputStr = inJson.toString();
	
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			// call WebService
			UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=getQuestionInfo",inputStr, null);
			outString = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		//analysis outString
		JSONObject outJson = JSONObject.fromObject(outString);
		String retCode = outJson.getString("retCode");
		QuestionBean questionBean = new QuestionBean();
		//judge result
		if (retCode.equals("0100")) {
			
			questionBean.setQuestionId(outJson.getString("questionId"));
			questionBean.setQuestionTitle(outJson.getString("questionTitle"));
			questionBean.setQuestionDetail(outJson.getString("questionDetail"));
			questionBean.setQuestioner(outJson.getString("questioner"));
			questionBean.setQuestionTime(outJson.getString("questionTime"));
			questionBean.setQuestionClose(outJson.getString("questionClose"));
			questionBean.setBestAnswerIndex(outJson.getString("bestAnswerIndex"));
			
			request.setAttribute("resource", questionBean);
			return mapping.findForward("succ");
		} else {
			String insertSubjectIndustry = "查看失败";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../questionListPre.do");
			return mapping.findForward("fail");
		}
	}
}
